Skip to content

Export unstable_serialize from SWR #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 17, 2021
Merged

Export unstable_serialize from SWR #1337

merged 4 commits into from
Aug 17, 2021

Conversation

shuding
Copy link
Member

@shuding shuding commented Aug 12, 2021

SWR handles key serialization so it can just pass a string to the cache provider. This makes all the interfaces simpler. However it makes getting data from the cache (or mutating a specific key) tricky, because the serialization util isn't exposed as an API.

Previously we had #1257 to expose the serialization of the useSWRInfinite key. This PR unifies the API name, as well as exposing the serialize API of useSWR.

Related: #1324, #1257.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 12, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f6b20ef:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-SSR Configuration

@shuding shuding marked this pull request as ready for review August 12, 2021 20:12
@shuding shuding requested a review from huozhi as a code owner August 12, 2021 20:12
}

export const getInfiniteKey = (getKey: KeyLoader<any>) => {
export const serialize = (getKey: KeyLoader<any>) => {
Copy link
Member

@huozhi huozhi Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should return an array here containing all the custom prefixed keys like $ctx, $len stuff?
That would be easier to access any of them

const serialize = (getKey) => {
  const firstPageKey = INFINITE_PREFIX + getFirstPageKey(getKey)
  let contextCacheKey, pageSizeCacheKey
  if (firstPageKey) {
     contextCacheKey = ..
     pageSizeCacheKey = ..
  }
  return [firstPageKey, contextCacheKey, pageSizeCacheKey]
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for now. I'm actually worried about exposing too many implementation details, we should do enough research about use cases (e.g. why do they need to access the infinite context?) before thinking about exposing those.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the different return type is also bit confusing for new uers (personally), but it's safe to carry on. We can revisit it later.

Would be nice if explicit return type is declared here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a good point. We should probably only return the data key from serialize.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unstable_serialize?

@huozhi huozhi changed the title Export serialize from SWR Export unstable_serialize from SWR Aug 17, 2021
Copy link
Member

@huozhi huozhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@huozhi huozhi removed the on hold label Aug 17, 2021
@huozhi huozhi merged commit 26da5f5 into master Aug 17, 2021
@huozhi huozhi deleted the export-serialize branch August 17, 2021 19:08
@piotr-cz
Copy link

piotr-cz commented Oct 5, 2021

This is helpful, however there is no way to retrieve errorKey and isValidatingKey values.

I'm using persistent storage for cache and once an item is removed, I'd like to remove it completely from persistent cache as well.

At this moment I'm using:

export function deleteCacheEntry(cache: Cache, key: string): void {
  const keys = [
    key,
    '$err$' + key, // errorKey
    '$req$' + key, // isValidatingKey
  ]

  keys.forEach(key =>
    cache.delete(key)
  ))
}

One prefixes change, such code won't have any effect.

@shuding
Copy link
Member Author

shuding commented Oct 6, 2021

I think one way to simplify all this is, using only 1 key for all states related to that resource and keep those states as object values:

// old
cache.set(key, data)
cache.set(errorKey, error)
cache.set(validatingKey, isValidating)

// new
cache.set(key, { data, error, isValidating })

This will surely be a breaking change but it would be great to add it.

@piotr-cz
Copy link

piotr-cz commented Oct 6, 2021

@shuding good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants